package com.dmx.platform.action;

import java.util.Date;
import java.util.List;

import com.dmx.fw.constant.ViewConstant;
import com.dmx.fw.util.DateTools;
import com.dmx.fw.web.struts2.action.CommonQueryAction;
import com.dmx.platform.Constants;
import com.dmx.platform.bean.PriceBean;
import com.dmx.platform.entity.TDetailinfo;
import com.dmx.platform.service.InformationService;
import com.dmx.platform.service.Message;
import com.dmx.platform.service.NotifyService;


/**
 *@author: Beni Yang
 *@date:2012-8-19
 *@function:
 */
@SuppressWarnings("serial")
public class InformationAction extends CommonQueryAction {

	private static final String BATCH_DELETE = "batchDelete";
	private static final String PUBLISH="publish";
	private static final String UNPUBLISH="unPublish";
	private TDetailinfo detailInfo;
	private List<PriceBean> priceBean;
	private InformationService infoService;

	private String[] ids;
	private String id;
	private String q_type;
	private String q_name;
	private String q_startDate;
	private String q_endDate;
	private String q_dstatus;
	

	// actions begin

	public String execute() throws Exception {
		super.busiId = "searchMyInfo";

		if (ViewConstant.QUERY.equals(super.act)) {
			doQuery();
		} else if (ViewConstant.REQUERY.equals(super.act)) {
			doReQuery();
		} else if (ViewConstant.DELETE.equals(super.act)) {
			this.doDelete();
		} else if (BATCH_DELETE.equals(super.act)) {
			doBatchDelete();
		} else if (ViewConstant.ADDINPUT.equals(super.act)) {
			this.pageState = ViewConstant.ADDSTATUS;
			return doAddInput();
		} else if (ViewConstant.ADDSUBMIT.equals(super.act)) {
			return doAddSubmit();
		} else if (ViewConstant.EDITINPUT.equals(super.act)) {
			this.pageState = ViewConstant.EDITSTATUS;
			return doEditInput();
		} else if (ViewConstant.EDITSUBMIT.equals(super.act)) {
			return doEditSubmit();
		} else if (ViewConstant.VIEW.equals(super.act)) {
			this.pageState = ViewConstant.VIEWSTATUS;
			return doEditInput();
		}else if (PUBLISH.equals(super.act)) {
			 doOperate(0);
		}else if (UNPUBLISH.equals(super.act)) {
			 doOperate(1);
		}
		
		else {
			super.execute();
		}
		return SUCCESS;
	}

	public String doAddInput() {
		
		this.detailInfo=new TDetailinfo();
		this.detailInfo.setStatus(TDetailinfo.STATUS_INPUTING);
		
		return ViewConstant.ADDINPUT;
	}

	public String doAddSubmit() throws Exception {
		
		try{
			detailInfo.setRecordTime(new Date());
			detailInfo.setTBaseinfo(infoService.findBaseInfoById(this.getBusiDataId()));
			if(q_startDate!=null&& !q_startDate.equals("")){
				detailInfo.setStartTime(DateTools.string2Date(q_startDate, Constants.PATTERN1));
			}
			if(q_endDate!=null && !q_endDate.equals("")){
				detailInfo.setEndTime(DateTools.string2Date(q_endDate, Constants.PATTERN1));
			}
			infoService.saveOrUpdateDetailInfo(detailInfo);
			
//			update price plan information of this detail info.
			if(priceBean!=null){
				log.info("this detailinfo["+detailInfo.getDetailId()+"] owner price plan "+priceBean.size());
				if(!infoService.saveOrUpdatePricePlan(priceBean, detailInfo.getDetailId())){
					this.setErrorInfo(this.getText("保存定价段落失败，请重新登陆后重试！"));
					return doEditInput();
				}				
			}else{
				if(!infoService.deletePricePlanByOwnerId(detailInfo.getDetailId())){
					this.setErrorInfo(this.getText("删除订价段落失败，请重新登陆后重试！"));
					return doEditInput();
				}
			}
			
			setSuccessinfo(getText("info.addSuccess"));
			this.q_startDate=null;
			this.q_endDate=null;
			super.doReQuery();
		}catch(Exception e){
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
			return doEditInput();
		}
		return SUCCESS;
	}

	public String doEditInput() {
		
		if (this.id != null) {
			detailInfo = infoService.findDetailInfoById(id);
		}
		return ViewConstant.EDITINPUT;
	}

	public String doEditSubmit() throws Exception {
		
		try{
			
			detailInfo.setRecordTime(new Date());
			detailInfo.setTBaseinfo(infoService.findBaseInfoById(this.getBusiDataId()));
			if(q_startDate!=null&& !q_startDate.equals("")){
				detailInfo.setStartTime(DateTools.string2Date(q_startDate, Constants.PATTERN1));
			}
			if(q_endDate!=null && !q_endDate.equals("")){
				detailInfo.setEndTime(DateTools.string2Date(q_endDate, Constants.PATTERN1));
			}
			
			infoService.saveOrUpdateDetailInfo(detailInfo);
//			update price plan information of this detail info.
			if(priceBean!=null){
				log.info("this detailinfo["+detailInfo.getDetailId()+"] owner price plan "+priceBean.size());
				if(!infoService.saveOrUpdatePricePlan(priceBean, detailInfo.getDetailId())){
					this.setErrorInfo(this.getText("保存定价段落失败，请重新登陆后重试！"));
					return doEditInput();
				}				
			}else{
				if(!infoService.deletePricePlanByOwnerId(detailInfo.getDetailId())){
					this.setErrorInfo(this.getText("删除订价段落失败，请重新登陆后重试！"));
					return doEditInput();
				}
			}
			
			setSuccessinfo(getText("info.addSuccess"));
			this.q_startDate=null;
			this.q_endDate=null;
			super.doReQuery();
		}catch(Exception e){
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
			return doEditInput();
		}
		return SUCCESS;
	}

	public String doBatchDelete() throws Exception {
		if(ids!=null){
			infoService.batchDeleteDetailInfoByIds(ids);
			setSuccessinfo(getText("info.deleteSuccess"));
		}
		return super.doQuery();
	}

	public String doDelete() throws Exception {
		if (this.id != null) {
			infoService.deleteDetailInfoById(id);
		}
		setSuccessinfo(getText("info.deleteSuccess"));
		return super.doQuery();
	}
	
	public String doOperate(int ac) throws Exception {
		try{
		detailInfo=this.infoService.findDetailInfoById(id);
		detailInfo.setRecordTime(new Date());
			if(ac==0){
				detailInfo.setStatus(TDetailinfo.STATUS_PUBLISHED);
				this.infoService.saveOrUpdateDetailInfo(detailInfo);
				setSuccessinfo(detailInfo.getName()+" - 上架成功！");
			}else{
				detailInfo.setStatus(TDetailinfo.STATUS_UNPUBLISHED);
				this.infoService.saveOrUpdateDetailInfo(detailInfo);
				setSuccessinfo(detailInfo.getName()+" - 下架成功！");
//				NotifyService.outingQ.offer(new Message(detailInfo.getTBaseinfo().getMobile()
//						,detailInfo.getTBaseinfo().getBaseId()+"/"+this.getCurrOperator().getLogin()
//						,Message.TYPE_UNPUBLISH_ROOM
//						,detailInfo.getName()));
		}
		}catch(Exception e){
			log.error("Operate Detail information failed "+e.getMessage());
			this.setErrorInfo("操作失败，请刷新后重试！");
		}

		return super.doQuery();
	}

	
	

	public InformationService getInfoService() {
		return infoService;
	}

	public void setInfoService(InformationService infoService) {
		this.infoService = infoService;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getQ_type() {
		return q_type;
	}

	public void setQ_type(String qType) {
		q_type = qType;
	}

	public String getQ_name() {
		return q_name;
	}

	public void setQ_name(String qName) {
		q_name = qName;
	}

	public String getQ_startDate() {
		return q_startDate;
	}

	public void setQ_startDate(String qStartDate) {
		q_startDate = qStartDate;
	}

	public String getQ_endDate() {
		return q_endDate;
	}

	public void setQ_endDate(String qEndDate) {
		q_endDate = qEndDate;
	}

	public String getQ_dstatus() {
		return q_dstatus;
	}

	public void setQ_dstatus(String qDstatus) {
		q_dstatus = qDstatus;
	}


	public TDetailinfo getDetailInfo() {
		return detailInfo;
	}

	public void setDetailInfo(TDetailinfo detailInfo) {
		this.detailInfo = detailInfo;
	}


	public List<PriceBean> getPriceBean() {
		return priceBean;
	}

	public void setPriceBean(List<PriceBean> priceBean) {
		this.priceBean = priceBean;
	}





}